package site.user.action;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.ApplyBg;
import modules.recruit.form.ApplyBgForm;
import modules.recruit.service.ApplyBgService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.core.service.EntityService;

public class ApplyBgActionSite extends ApplyCommonStep {
	
	private ApplyBgService applyBgService;

	public EntityService getEntityService() {
		return applyBgService;
	}

	public ApplyBgService getApplyBgService() {
		return applyBgService;
	}

	public void setApplyBgService(ApplyBgService applyBgService) {
		this.applyBgService = applyBgService;
	}

	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form,request);
		if(entity == null && applyBase != null){
			entity = applyBase.getBackground();
		}
		//不是由ID参数得到的entity
		if (id == null && entity != null) {
			ApplyBg applyBg = (ApplyBg)entity;
			ApplyBgForm applyBgForm = (ApplyBgForm) form;
			applyBgForm.setId(applyBg.getId());
			id = applyBg.getId();			
		}
	}

	/**
	 * 显示修改对象Form的Action函数.
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		bindForm(form, entity);
		ApplyBgForm applyBgForm = (ApplyBgForm)form;
		applyBgForm.setApplyBaseId(applyBaseId);
		Calendar cal=Calendar.getInstance();
		request.setAttribute("currentYear",String.valueOf(cal.get(Calendar.YEAR)));
		return handleEdit(mapping,form,request,response);
	}
	
	protected void doInitEntity(HttpServletRequest request, ActionForm form) {
		super.doInitEntity(request, form);
		if(isCreateOperation){
			ApplyBg applyBg = (ApplyBg) entity;
			applyBg.setBase(applyBase);
			entity = applyBg;
		}
	}

	/**
	 * 保存对象的Action函数.
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (!validateForm(mapping, form, request))
			return handleFormErrors(mapping, form, request, response);
		
		doInitEntity(request, form);

		doSaveEntity(request, form);

		String direction = request.getParameter("direction");
		if (direction != null && direction.equals("next"))
			return new ActionForward(
					"/site/user/applyManager.do?method=edit&applyBaseId="
							+ applyBaseId.toString() + "&position=1", true);
		else if(direction != null && direction.equals("last"))
			return new ActionForward(
					"/site/user/applyFinance.do?method=edit&applyBaseId="
							+ applyBaseId.toString(), true);
		else{
			saveMessage(request, "entity.saved");
			return handleEdit(mapping,form,request,response);
		}
	}
}
